package com.duobaoyu.dby.supply.admin.controller.config;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.core.validator.groups.Add;
import com.duobaoyu.dby.common.core.validator.groups.Edit;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceAfterSaleReasonAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.AfterSaleReasonDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.AfterSaleReasonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 售后原因配置
 *
 * @Author tianxiaoqiang
 * @Date 2022/7/29 19:28
 * @Version 1.0
 * @Description 售后原因配置
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/after-sale-reason")
@Api(value = "after-sale-reason", tags = "售后原因配置")
public class AfterSaleReasonController {

	private final ResourceAfterSaleReasonAdminClient resourceAfterSaleReasonAdminClient;

	/**
	 * 添加售后原因配置
	 *
	 * @param dto
	 * @return
	 */
	@PostMapping("/add")
	//@PreAuthorize("@pms.hasPermission('supply_reason_add')")
	@ApiOperation(value = "添加售后原因配置", notes = "添加售后原因配置")
	@SupplySysLog(value = "添加售后原因配置",operationType = OperationTypeEnum.INTERT)
	public R<Boolean> add(@Validated(Add.class) @RequestBody AfterSaleReasonDTO afterSaleReason) {

		return R.ok(resourceAfterSaleReasonAdminClient.createAfterSaleReason(afterSaleReason));
	}

	/**
	 * 修改售后原因配置
	 *
	 * @param id
	 * @param dto
	 * @return
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改售后原因配置", notes = "修改售后原因配置")
	@SupplySysLog(value = "修改售后原因配置",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> update(@Validated(Edit.class) @RequestBody AfterSaleReasonDTO afterSaleReason) {

		return R.ok(resourceAfterSaleReasonAdminClient.updateAfterSaleReason(afterSaleReason));
	}

	/**
	 * 删除售后原因配置
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value = "删除售后原因配置", notes = "删除售后原因配置")
	@SupplySysLog(value = "删除售后原因配置",operationType = OperationTypeEnum.DELETE)
	public R<Boolean> delete(@NotNull(message = "售后原因配置ID不能为空") @PathVariable("id") Long id) {

		return R.ok(resourceAfterSaleReasonAdminClient.deleteAfterSaleReason(id));
	}

	/**
	 * 查询单个售后原因配置信息
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "查询单个售后原因配置信息", notes = "根据id查询单个售后原因配置信息")
	@SupplySysLog(value = "查询单个售后原因配置信息",operationType = OperationTypeEnum.SELECT)
	public R<AfterSaleReasonVO> queryOne(@NotNull(message = "售后原因配置ID不能为空") @PathVariable("id") Long id) {

		return R.ok(resourceAfterSaleReasonAdminClient.queryAfterSaleReason(id));
	}

	/**
	 * 分页查询售后原因配置信息
	 *
	 * @param dto
	 * @return
	 */
	@PostMapping("/page")
	@ApiOperation(value = "分页查询售后原因配置信息", notes = "分页查询售后原因配置信息")
	@SupplySysLog(value = "获取缓存售后原因配置分页列表",operationType = OperationTypeEnum.SELECT)
	public R<Page<AfterSaleReasonVO>> page(@RequestBody AfterSaleReasonDTO dto) {

		return R.ok(resourceAfterSaleReasonAdminClient.selectPage(dto));
	}

	/**
	 * 获取售后原因配置缓存
	 *
	 * @param afterSaleTypes
	 * @return
	 */
	@PostMapping("/cache/saleReason")
	@ApiOperation(value = "获取售后原因配置缓存", notes = "获取售后原因配置缓存")
	@SupplySysLog(value = "获取售后原因配置缓存",operationType = OperationTypeEnum.SELECT)
	public R<Map<Integer, List<AfterSaleReasonVO>>> getAfterSaleReasonCacheByAfterSaleTypes(@RequestParam(value = "afterSaleTypes", required = false) List<Integer> afterSaleTypes) {

		return R.ok(resourceAfterSaleReasonAdminClient.getAfterSaleReasonCacheByAfterSaleTypes(afterSaleTypes));
	}
}
